package com.home.generator.common.utils;


import java.nio.ByteBuffer;
import java.nio.charset.Charset;

/**
 * Description: 类型转换器
 * Created by 王大宸 on 2020-06-07 23:30
 * Created with IntelliJ IDEA.
 */
public class Convert {

    public static final Charset CHARSET_UTF_8 = Charset.forName("UTF-8");

    public static String utf8Str(Object obj) {
        return str(obj, CHARSET_UTF_8);
    }

    public static String str(Object obj, Charset charset) {
        if (null == obj) {
            return null;
        }

        if (obj instanceof String) {
            return (String) obj;
        } else if (obj instanceof byte[] || obj instanceof Byte[]) {
            return str((Byte[]) obj, charset);
        } else if (obj instanceof ByteBuffer) {
            return str((ByteBuffer) obj, charset);
        }
        return obj.toString();
    }

}
